package de.lulusoft.anothertorrent.core.servers;

import java.io.Serializable;

import de.lulusoft.anothertorrent.core.exceptions.AnotherTorrentException;
import de.lulusoft.anothertorrent.core.rtorrent.RTorrentManager;

public class Server implements Serializable {

	private static final long serialVersionUID = 2191111770885746140L;
	
	private String url = "";
	private ServerType type = null;
	private String name = "";
	private String host = "";
	private boolean I8Support = false;
	private int port = 5000;
	transient private volatile RTorrentManager manager = null;
	transient private volatile Long upRate;
	transient private volatile Long downRate;
	transient private volatile Long uploadRate;
	transient private volatile Long downloadRate;
	transient private volatile String clientVersion;
	transient private volatile String libVersion;
	
	public Server(ServerType type, String name, String url) throws AnotherTorrentException {
		super();
		this.type = type;
		this.name = name;
		this.url = url;
		manager = new RTorrentManager(this);
	}
	
	public Server(ServerType type, String name, String host, int port) throws AnotherTorrentException {
		super();
		this.type = type;
		this.name = name;
		this.host = host;
		this.port = port;
		manager = new RTorrentManager(this);
	}

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public int getPort() {
		return port;
	}

	public void setPort(int port) {
		this.port = port;
	}
	
	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public ServerType getType() {
		return type;
	}

	public void setType(ServerType type) {
		this.type = type;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public RTorrentManager getRTorrentManager() throws AnotherTorrentException
	{
		if (manager == null)
			manager = new RTorrentManager(this);
		return manager;
	}
	
	public Long getUpRate()
	{
		return upRate;
	}
	
	public Long getDownRate()
	{
		return downRate;
	}
	
	public Long getUploadRateLimit()
	{
		return uploadRate;
	}
	
	public Long getDownloadRateLimit()
	{
		return downloadRate;
	}
	
	public void setUploadRateLimit(Long rate) throws AnotherTorrentException
	{
		manager.setGlobalUploadRate(rate);
	}
	
	public void setDownloadRateLimit(Long rate) throws AnotherTorrentException
	{
		manager.setGlobalDownloadRate(rate);
	}
	
	public String getClientVersion()
	{
		return clientVersion;
	}
	
	public String getLibVersion()
	{
		return libVersion;
	}
	
	public boolean isI8Support() {
		return I8Support;
	}

	public void setI8Support(boolean i8Support) {
		I8Support = i8Support;
	}

	public void updateGlobalStats() throws AnotherTorrentException
	{
		upRate = manager.getGlobalUpRate();
		downRate = manager.getGlobalDownRate();
		uploadRate = manager.getGlobalUploadRate();
		downloadRate = manager.getGlobalDownloadRate();
		clientVersion = manager.getGlobalClientVersion();
		libVersion = manager.getGlobalLibVersion();
	}
	
}
